<template>
	<view>

		<!-- 搜索部分 -->
		<view class="search_div">
			<view class="search_border">
				<input class="et_word" maxlength="24" placeholder="请输入关键字" :value="word" @input="onInputWord" />
				<view style="color: #ccc;">|</view>
				<view style="padding: 24rpx 36rpx 24rpx 24rpx;" @click.stop="onSearch">搜索</view>
			</view>
		</view>

		<view class="tab_div">
			<view class="tab_item" @click.stop="onSelStatus('-1')">
				<view :class="curr_tab == '-1' ? 'tab_txt_sel' : 'tab_txt'">全部</view>
			</view>
			<view class="tab_item" @click.stop="onSelStatus('1')">
				<view :class="curr_tab == '1' ? 'tab_txt_sel' : 'tab_txt'">待安装</view>
			</view>
			<view class="tab_item" @click.stop="onSelStatus('2,6')">
				<view :class="curr_tab == '2,6' ? 'tab_txt_sel' : 'tab_txt'">待业务审核</view>
			</view>
			<view class="tab_item" @click.stop="onSelStatus('3')">
				<view :class="curr_tab == '3' ? 'tab_txt_sel' : 'tab_txt'">待财务审核</view>
			</view>
			<view class="tab_item" @click.stop="onSelStatus('4')">
				<view :class="curr_tab == '4' ? 'tab_txt_sel' : 'tab_txt'">业务拒绝</view>
			</view>
		</view>

		<view style="width: 100%;height: 1rpx;background-color: #eee;"></view>

		<view class="install_list_div">
			<view class="install_item" v-for="(item,index) in install_list" :key="item.id">
				<view style="display: flex;justify-content: space-between;">
					<view>预约时间：{{timestampToDateFormat(item.install_date)}}</view>
					<view style="color: orangered;font-weight: bold;">{{getStatusTxt(item.status)}}</view>
				</view>
				<view>安装型号：{{item.ts}}</view>
				<view>锁体型号：{{item.lock_ts}}</view>
				<view>客户地址：{{item.address}}</view>
				<view>客户电话：<span @click.stop="onPhone(item)" style="color: deepskyblue;text-decoration: underline;font-weight: bold;">{{item.mobile}} (点击拨打)</span></view>
				<view>客户要求：{{item.remark}}</view>
				<view>赠品：{{item.gift}}</view>
				<view>销售人员：{{item.salesperson}}</view>
				<view v-if="item.anzhuang && item.anzhuang.tel">安装人员：{{item.anzhuang.name || item.anzhuang.tel}}</view>
				<!-- <view>抖音单号：{{item.dy_sn || '未填写'}}</view> -->
				<view v-if="item.status == 4 || item.status == 6" style="font-weight: bold;color: red;">
					拒绝原因：{{item.ye_yj || item.cw_yj}}
				</view>
				<view v-if="item.status == 99" style="font-weight: bold;color: red;">
					作废原因：{{item.cancel_text}}
				</view>

				<view class="op_btn_div">
					<view class="btn2" @click.stop="onEdit(item)">编辑</view>
					<view class="btn1" @click.stop="onDetail(item)">查看详情</view>
					<view v-if="item.status == 2 || item.status == 6" class="btn2" @click.stop="onDetail(item)">审核</view>
					<view v-if="item.status == 0 || item.status == 1" class="btn3" @click.stop="onCancelOrder(item)">作废</view>
				</view>

			</view>

			<view v-if="!install_list || install_list.length <= 0" style="width: 100%;text-align: center;margin: 10vh 0 0 0;">
				暂无数据~
			</view>

		</view>

	</view>
</template>

<script>
	import { getUInfo, getStatusTxt, timestampToYMDHM } from '@/util/util.js'
	export default {
		data() {
			return {
				install_list: [], // 安装列表
				curr_tab: '-1', // 记录当前选择的tab代表的状态
				word: '', // 搜索关键字

				page: 1,
				limit: 20,
			}
		},

		mounted() {
			// 事件总线:收到重新申请车费的消息
			uni.$on('onReApplyFreight', data => {
				console.log('监听到事件来自onReApplyFreight,携带参数 data =', data);
				that.getApplyList()
			})
		},

		onShow() {
			let that = this
			that.page = 1
			that.install_list = []
			this.getApplyList()
		},

		// 下拉刷新
		onPullDownRefresh() {
			this.page = 1
			this.word = ''
			this.install_list = []
			this.getApplyList()
		},

		// 到达底部
		onReachBottom() {
			console.log('已到达底部...');
			this.page++
			this.getApplyList()
		},

		methods: {

			// 点击搜索
			onSearch() {
				this.page = 1
				this.install_list = []
				this.getApplyList()
			},

			// 监听:输入关键字
			onInputWord(e) {
				this.word = e.detail.value
			},

			// 编辑
			onEdit(item) {
				// console.log('编辑', item.address);
				uni.$emit('onEdit', { install_info: item })
				uni.switchTab({ url: '/pages/index/index' })
			},

			// 拨打电话
			onPhone(item) {
				if (!item || !item.mobile) {
					uni.showToast({ icon: 'none', title: '号码有误!' })
				}
				uni.makePhoneCall({ phoneNumber: `${item.mobile}` })
			},

			// 作废
			onCancelOrder(item) {
				uni.showModal({
					title: '提示',
					placeholderText: `请填写作废原因`,
					editable: true,
					confirmText: '作废',
					confirmColor: '#ff0000',
					success: (res) => {
						if (res.confirm) {
							if (!res.content) {
								uni.showToast({ icon: 'error', title: '请填写原因', duration: 2900 })
								return
							}
							this.cancelOrder(res.content, item)
						} else {
							// uni.showToast({ icon: 'none', title: '未提交' })
						}
					}
				})
			},

			// 查看详情
			onDetail(item) {
				uni.navigateTo({
					url: `/pages/order_detail/order_detail?detail=${JSON.stringify(item)}`
				})
			},

			// -1 全部 
			onSelStatus(status) {
				this.curr_tab = status
				this.page = 1
				this.install_list = []
				this.getApplyList()
			},

			// 接口:作废安装需求
			async cancelOrder(cancel_text, item) {
				let that = this
				let token = getUInfo().token
				uni.showLoading({ title: '处理中', mask: true, })
				let res = await this.$cancelOrder({ cancel_text, id: item.id })
				// console.log('$cancelOrder.res =', res);
				if (res.code == 1) {
					this.getApplyList() // 操作成功 刷新数据
					uni.showModal({
						title: '提示',
						content: `已作废！`,
						showCancel: false,
						success: (res) => {
							if (res.confirm) {}
						}
					})
				} else {
					uni.showToast({ icon: 'none', title: res.msg })
				}
			},

			// 接口:审批列表(安装列表)
			async getApplyList() {
				let that = this
				let token = getUInfo().token
				let status = that.curr_tab
				uni.showLoading({ title: '加载中', mask: true, })
				let { code, msg, data } = await this.$getInstallList({ status, page: this.page, limit: this.limit, keyword: this.word })
				if (code == 1) {
					if (!data || data.length <= 0) {
						uni.showToast({ icon: 'none', title: '暂无更多数据' })
						return
					}
					this.install_list = this.install_list.concat(data)
					this.install_list = this.install_list.map((item, index) => {
						if (!item.zp || item.zp == 'null') {
							item.zp = ''
						}
						if (!item.wx || item.wx == 'null') {
							item.wx = ''
						}
						if (!item.hx || item.hx == 'null') {
							item.hx = ''
						}
						if (!item.zb || item.zb == 'null') {
							item.zb = ''
						}
						return item
					})
				} else {
					this.$toast(res.msg)
				}
			},

			getStatusTxt(status) {
				return getStatusTxt(status)
			},

			timestampToDateFormat(ts) {
				return timestampToYMDHM(1000 * ts)
			},

		}
	}
</script>

<style scoped>
	.tab_div {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 88rpx;
		position: fixed;
		background-color: white;
		margin: 80rpx 0 0 0;
	}

	.tab_item {
		flex-grow: 1;
		text-align: center;
		height: 100%;
		line-height: 88rpx;
	}

	.tab_txt {
		font-size: 29rpx;
		font-weight: normal;
		color: #222;
	}

	.tab_txt_sel {
		font-size: 29rpx;
		font-weight: bold;
		color: #1890FF;
	}

	.search_div {
		position: fixed;
		height: 90rpx;
		width: calc(100%);
		background-color: white;
		z-index: 1;
	}

	.search_border {
		display: flex;
		align-items: center;
		width: calc(100% - 24rpx);
		height: calc(100% - 24rpx);
		margin: 12rpx 12rpx 12rpx 12rpx;
		border: solid 1rpx #ccc;
		border-radius: 888rpx;
	}

	.et_word {
		flex-grow: 1;
		padding: 0 0 0 48rpx;
	}

	.install_list_div {
		display: flex;
		flex-direction: column;
		width: 100vw;
		margin: 160rpx 0 0 0;
	}

	.install_item {
		display: flex;
		flex-direction: column;
		/* height: 300rpx; */
		width: calc(100% - 80rpx);
		margin: 20rpx 20rpx 0 20rpx;
		padding: 20rpx;
		background-color: rgba(249, 233, 198, 1);
		/* background-color: rgba(237, 216, 171, 1); */
		/* background-color: rgba(219, 38, 29, 0.3); */
		overflow: scroll;
		/* overflow: hidden; */
		border-radius: 16rpx;
		font-size: 30rpx;
		color: black;
	}

	/* 操作按钮容器 */
	.op_btn_div {
		display: flex;
		justify-content: flex-end;
		align-items: center;
		margin: 20rpx 0 0 0;
	}

	.btn1 {
		width: 166rpx;
		height: 70rpx;
		line-height: 70rpx;
		text-align: center;
		background-color: deepskyblue;
		color: white;
		margin: 0 0 0 20rpx;
		border-radius: 10rpx;
	}

	.btn2 {
		width: 166rpx;
		height: 70rpx;
		line-height: 70rpx;
		text-align: center;
		background-color: yellowgreen;
		color: white;
		margin: 0 0 0 20rpx;
		border-radius: 10rpx;
	}

	.btn3 {
		width: 166rpx;
		height: 70rpx;
		line-height: 70rpx;
		text-align: center;
		background-color: orangered;
		color: white;
		margin: 0 0 0 20rpx;
		border-radius: 10rpx;
	}
</style>